<template>
  <view class="content">
    <view class="topBox">
      <h3>WELCOME</h3>
      <h3>视力防控系统</h3>
    </view>
    <view class="inputBox">
      <view class="ipt">
        <h4>账号</h4>
        <input type="text" v-model="username" placeholder="请输入账号" />
      </view>
      <view class="ipt">
        <h4>密码</h4>
        <input type="password" v-model="password" placeholder="请输入密码" />
      </view>
      <button @tap="login" class="loginBtn">登录</button>
    </view>
  </view>
</template>

<script>
import * as CaptchaApi from '@/api/captcha';
import { isEmpty } from '@/utils/verify.js';

export default {
  data() {
    return {
      title: '视力防控系统',
      subTitle: '欢迎回来，开始工作吧！',
      captcha: null,
      username: '',
      password: '',
      captchaCode: '',
      uuid: ''
    };
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    // 获取图形验证码
    getCaptcha() {
      CaptchaApi.image().then(result => {
        this.captcha = 'data:image/gif;base64,' + result.img;
        this.uuid = result.uuid;
      });
    },
    // 验证表单内容
    validItem() {
      if (isEmpty(this.username)) {
        uni.$u.toast('请输入登录用户名');
        return false;
      }
      if (isEmpty(this.password)) {
        uni.$u.toast('请输入登录密码');
        return false;
      }
      return true;
    },
    // 确认登录
    login() {
      if (!this.validItem()) return;

      this.$store.dispatch('Login', {
        username: this.username,
        password: this.password,
        code: this.captchaCode,
        uuid: this.uuid
      })
      .then(() => {
        uni.switchTab({
          url: '/pages/index/index',
          fail(err) {
            console.error(err);
          }
        });
      })
      .catch(() => {
        this.captchaCode = '';
        this.getCaptcha();
      });
    }
  }
};
</script>

<style scoped>
.content {
  height: 100vh;
  background: linear-gradient(to right, #97f3c2, #67e3cb);
  background-size: cover;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.topBox {
  font-size: 50rpx;
  color: #000000;
  padding: 80rpx 50rpx;
}

h3 {
  margin-bottom: 10rpx;
}

.inputBox {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 75vh;
  background-color: #fff;
  border-top-left-radius: 40rpx;
  border-top-right-radius: 40rpx;
  padding: 60rpx;
  box-sizing: border-box;
}

.ipt {
  margin-bottom: 50rpx;
}

.ipt h4 {
  margin-bottom: 20rpx;
  font-size: 36rpx;
  color: #333;
}

.ipt input {
  border-bottom: 1px solid #dedede;
  padding-bottom: 20rpx;
  font-size: 28rpx;
}

.loginBtn {
  margin-top: 50rpx;
  line-height: 85rpx;
  text-align: center;
  background: #67e3cb;
  border-radius: 40rpx;
  color: #000000;
  font-size: 35rpx;
}
</style>